﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLLFactory;
using DataMode;
using IBLL;
using System.IO;

namespace Face.Web.VideoPage
{
    public partial class VideoDisplay : System.Web.UI.Page
    {
        public string MyVideosManageURL = "MyVideosManage.aspx";
        public string VideoTitle = "中央电视台新闻联播";
        public string VideoUploadTime = "10小时前";
        public string VideoPlayTime = "100次";
        public string VideoOwner = "FG";
        public string VideoAbstract = "这是视频介绍，新闻联播是CCTV的重头节目，这是视频介绍，新闻联播是CCTV的重头节目，这是视频介绍，新闻联播是CCTV的重头节目，这是视频介绍，新闻联播是CCTV的重头节目。";
        public string VideoFileURL = "/js/test.flv";
        public string VideoImgURL = "/img/新闻联播.png";
        public string AlbumOwner = "FG";
        public string AlbumUpdateTime = "1小时前";
        public string AlbumName = "新闻联播";
        public string AlbumImg = "/img/视频信息图.jpg";
        public string LikeNum = "";
        public string DislikeNum = "";
        public int pageNum = 1;
        public int NumOfTotalPage = 1;
        public string thisUsername = "null_user_name";
        public int replyUserID;
        public int replyMessageID;
        public string replyUserName;
        public string DeleteReplyUserName;


        public int ThisAlbumID = -1;
        public int ThisVideoID = -1;

        public DataMode.video_album thisAlbumInfo;
        public DataMode.video thisVideoInfo;
        
        //通过bll工厂模式调用业务层
        private static IBLL.IMessagesInVideoDisplay bll = BLLFactory.DataAccess.CreateMessagesInVideoDisplay();
        private static IBLL.IVideoInVideoPage bllVideo = BLLFactory.DataAccess.GetVideoInVideoPage();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["VideoID"] != null)
            {
                int test;
                if (System.Int32.TryParse(Request.QueryString["VideoID"], out test))
                    ThisVideoID = test;
                
            }
            if (Session["user"] != null)
            {
                thisUsername = Session["user"].ToString();
            }
            
            if (ThisVideoID > 0)
                NumOfTotalPage = bll.GetMessagesTotalPageNumByVideoID(ThisVideoID);
                thisVideoInfo = bllVideo.GetVideoInfoByVideoID(ThisVideoID);
            if (thisVideoInfo != null)
            {
                VideoTitle = thisVideoInfo.video_name;
                VideoUploadTime = thisVideoInfo.load_time.ToString();
                VideoPlayTime = thisVideoInfo.play_num.ToString();
                VideoOwner = thisVideoInfo.username;
                VideoAbstract = thisVideoInfo.video_description;
                LikeNum = thisVideoInfo.like_num.ToString();
                DislikeNum = thisVideoInfo.dislike_num.ToString();
                VideoFileURL = thisVideoInfo.video_path;
                VideoImgURL = thisVideoInfo.video_img;
                ThisAlbumID = (int)thisVideoInfo.video_album_id;
                thisAlbumInfo = bllVideo.GetThisAlbumInfoByAlbumID(ThisAlbumID);
            }
            if (thisAlbumInfo != null)
            {
                AlbumOwner = thisAlbumInfo.album_user;
                AlbumUpdateTime = thisAlbumInfo.album_create_time.ToString();
                AlbumName = thisAlbumInfo.album_name;
                AlbumImg = thisAlbumInfo.album_path;
            }

            if (!IsPostBack)
            {
                bindDataToVideosInAlbum();
                bindData();
                Cache.Remove("pageNum");
                Cache.Insert("pageNum", pageNum);
            }
            else
            {
                pageNum = (int)Cache["pageNum"];
            }
            
        }


        private void bindData()
        {
            //调用业务层
            Message_info.DataSource = bll.GetMessagesByVideoIDOnePage(ThisVideoID, pageNum);
            Message_info.DataBind();
        }

        private void add()
        {
            //调用业务层
            this.thisVideoInfo.like_num = thisVideoInfo.like_num++;
        }
        private void delete()
        {
            //调用业务层
            this.thisVideoInfo.dislike_num = thisVideoInfo.dislike_num++;
        }


        public void btn_SendMessage_Click(object sender, EventArgs e)
        {
            //新建一个message对象
            message msg = new message();
            msg.username = thisUsername;
            msg.message_time = DateTime.Now;
            msg.message_content = tb_Message.InnerText;
            if (Cache["replyUserName"] != null && Cache["replyUserName"] != "")
            {
                msg.reply_id = (int)Cache["replyMessageID"];
                msg.reply_username = (string)Cache["replyUserName"];
            }
            msg.video_id = ThisVideoID;
            //调用业务层处理
            bll.InsertMessage(msg);
            tb_Message.InnerText = "";
            bindData();
        }

        public void LastPageClick(object sender, EventArgs e)
        {
            if(pageNum > 1)
            {
                pageNum--;
                Cache.Insert("pageNum", pageNum);
                bindData();
            }
        }

        public void NextPageClick(object sender, EventArgs e)
        {
            NumOfTotalPage = bll.GetMessagesTotalPageNumByVideoID(ThisVideoID);
            if (pageNum < NumOfTotalPage)
            {
                pageNum++;
                Cache.Insert("pageNum", pageNum);
                bindData();
            }
        }

        protected void rpt_Message_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "btn_DeleteMessage")
            {
                bll.DeleteMessageByMessageID(System.Int32.Parse(e.CommandArgument.ToString()));
                bindData();
            }
            if (e.CommandName == "btn_ReplyMessage")
            {
                replyMessageID = System.Int32.Parse(e.CommandArgument.ToString());
                replyUserName = bll.GetUserNameOfMessage(replyMessageID);
                replyUserID = bll.GetUserIDByUserName(replyUserName);
                DeleteReplyUserName = "删除";
                Cache.Remove("replyUserName");
                Cache.Insert("replyUserName", replyUserName);
                Cache.Remove("replyUserID");
                Cache.Insert("replyUserID",replyUserID);
                Cache.Remove("replyMessageID");
                Cache.Insert("replyMessageID", replyMessageID);
            }
        }

        protected void btn_DeleteReplyName(object sender, EventArgs e)
        {
            replyUserName = "";
            replyUserID = -1;
            replyMessageID = -1;
            DeleteReplyUserName = "";
            Cache.Remove("replyUserName");
            Cache.Remove("replyUserID");
            Cache.Remove("replyMessageID");
        }

        private void bindDataToVideosInAlbum()
        {
            VideosInAlbum.DataSource = bllVideo.GetVideosByAlbumID(ThisVideoID);
            VideosInAlbum.DataBind();
        }

        protected void rpt_Videos_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        protected void btn_logout(object sender, EventArgs e)
        {
            Session.Remove("user");
            Response.Redirect("/login_and_signup/login.aspx");
        }
    }
}